<?php

/**
 * @author HungPV <phamvanhung0818@gmail.com>
 * @todo managment member model
 * @since 1.0
 */
class member extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    /**
     * get member by param
     * @param int $id
     * @param int $limit
     * @param int $offset
     * @return null
     */
    function get($id = -1, $limit = -1, $offset = 0) {
        if ($id == -1) {
            if ($limit == -1) {
                $query = $this->db->get_where('member');
            } else {
                $query = $this->db->get_where('member', $limit, $offset);
            }

            $result = $query->result();
            return $result;
        } else if ($id > 0) {
            $query = $this->db->get_where('member', array('id' => $id));
            $result = $query->result();
            if (count($result) > 0) {
                return $result[0];
            } else {
                return null;
            }
        }
    }

    /**
     * insert new member to database
     * @param string $name
     * @param string $position
     * @param string $description
     * @param string $image
     * @param string $facebook
     * @param string $skype
     * @param string $phone
     * @param string $email
     * @return boolean
     */
    function add($name, $position = '', $description = '', $image = '', $facebook = '', $skype = '', $phone = '', $email = '') {
        $member = array(
            'name' => $name,
            'position' => $position,
            'description' => $description,
            'image' => $image,
            'facebook' => $facebook,
            'skype' => $skype,
            'phone' => $phone,
            'email' => $email
        );
        $this->db->insert('member', $member);
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * update a member to database
     * @param type $id
     * @param type $position
     * @param type $description
     * @param type $image
     * @param type $facebook
     * @param type $skype
     * @param type $phone
     * @param type $email
     * @return boolean
     */
    function edit($id, $name, $position, $description, $image, $facebook, $skype, $phone, $email) {
        $member = array(
            'name' => $name,
            'position' => $position,
            'description' => $description,
            'image' => $image,
            'facebook' => $facebook,
            'skype' => $skype,
            'phone' => $phone,
            'email' => $email
        );
        $this->db->where('id', $id);
        $this->db->update('member', $member);
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * delete a member from database
     * @param int $id
     */
    function delete($id = 0) {
        $this->db->delete('member', array('id' => $id));
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

}

?>